﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ConcordWinManager
{
    public partial class AddressEditForm : Form
    {
        public Address address;

        private string mode;

        private Form parentForm;

        public AddressEditForm()
        {
            InitializeComponent();

            address = new Address();

            this.Text = string.Format("Адрес [{0}]", mode);

            cbxRegionType.SelectedIndex = 0;
            cbxNasPunktType.SelectedIndex = 0;
            cbxStreetType.SelectedIndex = 0;
        }

        public AddressEditForm(Form parentForm, string mode)
        {
            InitializeComponent();

            this.parentForm = parentForm;

            address = new Address();

            this.mode = mode;

            this.Text = string.Format("Адрес [{0}]", mode);

            cbxRegionType.SelectedIndex = 0;
            cbxNasPunktType.SelectedIndex = 0;
            cbxStreetType.SelectedIndex = 0;

            act = true;
        }


        public AddressEditForm(Form parentForm, string mode, int id)
        {
            InitializeComponent();

            this.parentForm = parentForm;

            address = new Address();

            this.mode = mode;

            this.Text = string.Format("Адрес [{0}]", mode);

            if (mode == "Изменение")
            {
                Tools.RunWithProgressIndicator(this, delegate { address.SelectById(id); });

                tbxIndex.Text = address.index;
                tbxRegionName.Text = address.regionName;
                tbxNasPunktName.Text = address.nasPunktName;
                tbxStreetName.Text = address.streetName;
                tbxHomeNumber.Text = address.homeNumber;
                tbxBuildNumber.Text = address.buildNumber;
                tbxFlatNumber.Text = address.flatNumber;


                switch (address.regionType)
                {
                    case "обл.":
                        cbxRegionType.Text = "Область";
                        break;
                    case "край":
                        cbxRegionType.Text = "Край";
                        break;
                    default:
                        cbxRegionType.Text = "<Другое>";
                        break;
                }

                switch (address.nasPunktType)
                {
                    case "г":
                        cbxNasPunktType.Text = "Город";
                        break;
                    case "пос":
                        cbxNasPunktType.Text = "Поселок";
                        break;
                    case "с":
                        cbxNasPunktType.Text = "Село";
                        break;
                    case "д":
                        cbxNasPunktType.Text = "Деревня";
                        break;

                    default:
                        cbxNasPunktType.Text = "<Другое>";
                        break;
                }

                switch (address.streetType)
                {
                    case "ул":
                        cbxStreetType.Text = "Улица";
                        break;
                    case "пр":
                        cbxStreetType.Text = "Проспект";
                        break;
                    case "пер":
                        cbxStreetType.Text = "Переулок";
                        break;
                    default:
                        cbxStreetType.Text = "<Другое>";
                        break;
                }
            }
            else
            {
                cbxRegionType.SelectedIndex = 0;
                cbxNasPunktType.SelectedIndex = 0;
                cbxStreetType.SelectedIndex = 0;
            }

            act = true;
            rebuildAddressText(this, null);
        }

        bool act = false;

        private void rebuildAddressText(object sender, EventArgs e)
        {
            //if (act)
            {
                address.index = tbxIndex.Text.Trim();
                address.regionName = tbxRegionName.Text.Trim();
                address.nasPunktName = tbxNasPunktName.Text.Trim();
                address.streetName = tbxStreetName.Text.Trim();
                address.homeNumber = tbxHomeNumber.Text.Trim();
                address.buildNumber = tbxBuildNumber.Text.Trim();
                address.flatNumber = tbxFlatNumber.Text.Trim();

                switch (cbxRegionType.Text)
                {
                    case "Область":
                        address.regionType = "обл.";
                        break;
                    case "Край":
                        address.regionType = "край";
                        break;
                    default:
                        address.regionType = "";
                        break;
                }

                switch (cbxNasPunktType.Text)
                {
                    case "Город":
                        address.nasPunktType = "г";
                        break;
                    case "Поселок":
                        address.nasPunktType = "пос";
                        break;
                    case "Село":
                        address.nasPunktType = "с";
                        break;
                    case "Деревня":
                        address.nasPunktType = "д";
                        break;

                    default:
                        address.nasPunktType = "";
                        break;
                }

                switch (cbxStreetType.Text)
                {
                    case "Улица":
                        address.streetType = "ул";
                        break;
                    case "Проспект":
                        address.streetType = "пр";
                        break;
                    case "Переулок":
                        address.streetType = "пер";
                        break;
                    default:
                        address.streetType = "";
                        break;
                }

                tbxAddressText.Text = address.RebuildAddressText();
            }
        }


        private void btnOK_Click(object sender, EventArgs e)
        {
            bool bSuccessfully = false;

            address.index = tbxIndex.Text.Trim();
            address.regionName = tbxRegionName.Text.Trim();
            address.nasPunktName = tbxNasPunktName.Text.Trim();
            address.streetName = tbxStreetName.Text.Trim();
            address.homeNumber = tbxHomeNumber.Text.Trim();
            address.buildNumber = tbxBuildNumber.Text.Trim();
            address.flatNumber = tbxFlatNumber.Text.Trim();
                        
            tbxAddressText.Text = address.RebuildAddressText();

            try
            {
                if (this.mode == "Изменение")
                {
                    Tools.RunWithProgressIndicator(this, delegate { address.Update(); });
                }
                else
                {
                    if (mode == "Добавление")
                    {
                        Tools.RunWithProgressIndicator(this, delegate { address.Insert(); });
                    }
                }

                bSuccessfully = true;
            }
            catch
            {
                ;
            }

            if (bSuccessfully)
            {
                this.Close();
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}